part of '../button.dart';

// 长方形的实心按钮，当鼠标浮现时，会有浮起的动画，浮起的方向可以自行设定
class LxSolidButton extends StatelessWidget {
  final Direction elevateDirection;
  final Widget child;
  final VoidCallback? onClick;
  final double? width;
  final double? height;
  final Color? color;
  const LxSolidButton({
    this.elevateDirection = Direction.up,
    this.width,
    this.height,
    this.color,
    required this.onClick,
    required this.child,
    super.key,
  });

  LxSolidButton.squareSvg({
    required String assetPath,
    required this.onClick,
    this.color,
    Color? svgColor,
    double ratio = 1,
    double size = 40,
    this.elevateDirection = Direction.up,
    super.key,
  })  : child = Center(
          child: LxSvgAsset(
            assetPath,
            color: svgColor ?? COLOR.black,
            ratio: ratio,
          ),
        ),
        height = size,
        width = size;

  LxSolidButton.text(
    String text, {
    super.key,
    this.height = 40,
    this.width = double.maxFinite,
    this.color,
    required this.onClick,
  })  : child = Center(
          child: Text(
            text,
            style: FONT.dingmao.copyWith(
              color: COLOR.black,
            ),
          ),
        ),
        elevateDirection = Direction.up;

  @override
  Widget build(BuildContext context) {
    Color color = this.color ?? COLOR.white;
    final bool isValid = onClick != null;
    if (!isValid) color = color.darken(0.88);

    return LxClickable(
      duration: const Duration(milliseconds: 150),
      onClick: onClick,
      builder: (ctrl) {
        // 根据不同的方向，确定不同的offset
        late final Offset offset;
        final delta = ctrl.valueRush * 3;
        switch (elevateDirection) {
          case Direction.up:
            offset = Offset(0, -delta);
            break;
          case Direction.down:
            offset = Offset(0, delta);
            break;
          case Direction.left:
            offset = Offset(-delta, 0);
            break;
          case Direction.right:
            offset = Offset(delta, 0);
            break;
        }

        return Transform.translate(
          offset: offset,
          child: Container(
            width: width,
            height: height,
            decoration: BoxDecoration(
                color: color,
                borderRadius: const BorderRadius.all(Radius.circular(4)),
                boxShadow: [
                  BoxShadow(
                    color: color.withOpacity(0.6),
                    blurRadius: 12 + ctrl.valueRush * 6,
                  ),
                ]),
            child: child,
          ),
        );
      },
    );
  }
}

enum Direction { up, down, left, right }
